package com.cloud.bssp.java8.lambda;

import lombok.Data;

import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @description： 构造函数引用
 * @author：weirx
 * @date：2021/10/19 17:50
 * @version：3.0
 */
public class ConstructorReference {

    @Data
    static class Student {
        private String name;
        private Integer age;
        private String address;

        public Student() {
        }

        public Student(String name) {
            this.name = name;
        }

        public Student(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        public Student(String name, Integer age, String address) {
            this.name = name;
            this.age = age;
            this.address = address;
        }
    }


    /**
     * description: 自定义三个参数的接口
     *
     * @return:
     * @author: weirx
     * @time: 2021/10/19 18:03
     */
    interface ThreeParamsFunction<T, U, P, R> {
        R apply(T t, U u, P p);
    }

    public static void main(String[] args) {
        // 无构造参数 ()->new Student() 转化成 Student::new
        Supplier<Student> supplier = Student::new;
        Student student = supplier.get();
        // 一个构造参数 (name)->new Student(name) 转化成 Student::new
        Function<String, Student> function = Student::new;
        function.apply("JACK");
        // 两个构造参数 (name,age)->new Student(name,age) 转化成 Student::new
        BiFunction<String, Integer, Student> biFunction = Student::new;
        biFunction.apply("JACK", 10);
        // 三个构造参数,没有提供三个构造参数的，需要自己写个接口
        // (name,age,address)->new Student(name,age,address) 转化成 Student::new
        ThreeParamsFunction<String, Integer, String, Student> threeParamsFunction = Student::new;
        threeParamsFunction.apply("JACK", 10, "Haerbin");
    }
}
